from  selenium import webdriver
from selenium.common import WebDriverException, InvalidArgumentException, NoSuchElementException
from selenium.webdriver.common.by import    By
import time

from selenium.webdriver.support.wait import WebDriverWait

from common.function import open_browser
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as  EC


class Base:
    def __init__(self,browser):
        self.driver=browser
        self.driver.maximize_window()

    def input_url(self,url):
        try:
            self.driver.get(url)
        except (WebDriverException, InvalidArgumentException):
            print('输入无效的网址')

    def select_option(self,locator,i):
        so = self.elelocate(locator)
        self.select = Select(so)
        if so is not None:
            self.select.select_by_index(i)
        else:
            print('没有找到下拉框')

    def select_option2(self,locator,value):
        so = self.elelocate(locator)
        self.select = Select(so)
        if so is not None:
            self.select.select_by_value(value)
        else:
            print('没有找到下拉框')

    def select_option3(self,locator,text):
        so = self.elelocate(locator)
        self.select = Select(so)
        if so is not None:
            self.select.select_by_visible_text(text)
        else:
            print('没有找到下拉框')


    def elelocate(self,locator, timeout =5):
        try:
            return WebDriverWait(self.driver,timeout=timeout).until(EC.presence_of_element_located(locator))
        except (NoSuchElementException):
            print('没有定位到元素')

    def click(self,locator):
            element=self.elelocate(locator)
            if element is not None:
                element.click()
            else:
                print('找不到元素')

    def switch_to_alert(self):
        self.driver.switch_to.alert()

    def send_keys(self,locator,msg):
        sd=self.elelocate(locator)
        if sd is not None:
            sd.send_keys(msg)
        else:
            print('找不到输入框')

    def  get_attribute(self,locator,value):
        ga=self.elelocate(locator)
        if ga is not None:
            ga.get_attribute(value)
            return ga
        else:
            print('找不到属性')


    def get_text(self,locator):
        gt = self.elelocate(locator)
        if gt is not None:
            print(gt.text)
        else:
            print('找不到属性')

    def clear(self,locator):
        cl=self.elelocate(locator)
        if cl is not None:
            cl.clear()
        else:
            print('找不到输入框')

    def scroll_low(self):
        time.sleep(2)
        js1 = 'window.scrollTo(0,document.body.scrollHeight)'
        return self.driver.execute_script(js1)

    def scroll_top(self):
        time.sleep(2)
        js2 = 'window.scrollTo(0,0)'
        return self.driver.execute_script(js2)

    def scroll_target(self,locator):
        time.sleep(2)
        target = self.elelocate(locator)
        return self.driver.execute_script('arguments[0].scrollIntoView();', target)


    def quit(self,seconds=3):
        time.sleep(seconds)
        self.driver.quit()

    def refresh(self):
        time.sleep(2)
        self.driver.refresh()
        time.sleep(2)

    def swtich_iframe(self,locator):#切换到指定的iframe
        frame=self.elelocate(locator)
        self.driver.switch_to.frame(frame)

    def switch_to_default_content(self):#切出frame
        self.driver.switch_to.default_content()

    def forward(self):
        time.sleep(2)
        self.driver.forward()

    def back(self):
        time.sleep(2)
        self.driver.back()

    def switch_alert(self):
        alert=self.driver.switch_to.alert
        print(alert.text)
        alert.accept()

        

if __name__ == '__main__':
    driver = open_browser()
    base =Base(driver)
    base.input_url('https://www.baidu.com')
    base.send_keys((By.ID, 'kw'), 'python')
    base.click((By.ID, 'su'))
    base.scroll_low()
    base.scroll_top()
    base.scroll_target((By.PARTIAL_LINK_TEXT,'菜鸟教程'))

    # base.elelocate((By.ID,'kw'))
    # base.get_attribute((By.ID,'su'),'id')
    # # base.send_keys((By.ID,'kw'),'啦啦啦啦')
    # base.get_text((By.LINK_TEXT,'新闻'))
    # base.click((By.ID,'su'))
    base.quit()




























